package com.mine.yyb.modules.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.mine.yyb.common.enums.YybModulesEnum;
import com.mine.yyb.common.utils.exception.MineErrorCode;
import com.mine.yyb.common.utils.exception.MineException;
import com.mine.yyb.common.utils.execl.MineExcelUtils;
import com.mine.yyb.common.web.YybBaseController;
import com.mine.yyb.modules.sys.bo.RyReportBO;
import com.mine.yyb.modules.sys.enums.SysCodeEnum;
import com.mine.yyb.modules.sys.service.IRyReportService;
import com.mine.yyb.modules.sys.vo.RyReportVO;

/**
 * <pre>
 * <一句话功能简述>
 * <功能详细描述>
 * </pre>
 * 
 * @author 姓名 工号
 * @version [版本号, 2017年6月12日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@RequestMapping(value = "/sys/ryreport")
public class RyReportControl extends YybBaseController
{
    @Autowired
    private IRyReportService iRyOrderService;
    
    @RequestMapping(value = "/importJpamzExcel")
    public ResponseEntity<String> importJpamzExcel(MultipartFile file, HttpServletRequest request,
        HttpServletResponse response, HttpSession session)
    {
        // 响应码
        String retCode = null;
        
        try
        {
            // 文件为空
            if (file == null)
            {
                throw new MineException(SysCodeEnum.ERROR_REPORT_FILE_EMPTY);
            }
            
            // 文件大小超过5M
            if (file.getSize() > 5120 * 1024)
            {
                throw new MineException(SysCodeEnum.ERROR_FILE_SIZE_TOO_LARGE);
            }
            
            MineExcelUtils mineImportExcel = new MineExcelUtils(file, 0, 0);
            // 读取excel文件里面的数据
            List<RyReportBO> orderList = mineImportExcel.getDataList(RyReportBO.class);
            List<RyReportBO> errorList = Lists.newArrayList();
            if (orderList.size() > 0)
            {
                // 先清空表
                iRyOrderService.truncate();
                
                for (RyReportBO reportBO : orderList)
                {
                    if (StringUtils.isBlank(reportBO.getOrderID()))
                    {
                        errorList.add(reportBO);
                        continue;
                    }
                    String result = null;
                    reportBO.setState("3");
                    RyReportVO temp = iRyOrderService.queryJpamzOrder(reportBO);
                    if (temp != null)
                    {
                        result = iRyOrderService.update(reportBO);
                    }
                    else
                    {
                        result = iRyOrderService.save(reportBO);
                    }
                    if (MineErrorCode.SUCCESS.name().equals(result))
                    {
                        errorList.add(reportBO);
                        continue;
                    }
                }
            }
            retCode = MineErrorCode.SUCCESS.name();
        }
        catch (MineException e)
        {
            logger.error(YybModulesEnum.SYS.name(), e);
            retCode = e.getErrorCode().name();
        }
        catch (Exception e)
        {
            logger.error(YybModulesEnum.SYS.name(), e);
            retCode = MineErrorCode.ERROR_INTERNAL.name();
        }
        
        return getResponseEntity(retCode, null);
    }
    
}
